package top.chmin.project.cloud.common.entity.column.relation.auto;

import org.springframework.stereotype.Repository;
import top.chmin.project.cloud.common.dao.BaseDao;
import top.chmin.project.cloud.common.entity.Condition;
import top.chmin.project.cloud.common.entity.Page;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * chmin
 * 2023-12-27 15:15:42
 * cloud-builder
 */
@Repository
public class EntityColumnRelationDao extends BaseDao<EntityColumnRelation> {

    @Override
    public int save(EntityColumnRelation model) {
        StringBuilder sb = new StringBuilder("INSERT INTO bs_entity_column_relation(");
// field column auto
// #bind.field.sourceFieldCode#
        sb.append("source_field_code,");
// $bind.field.sourceFieldCode$

// #bind.field.targetFieldCode#
        sb.append("target_field_code,");
// $bind.field.targetFieldCode$

// #bind.field.sourceTableCode#
        sb.append("source_table_code,");
// $bind.field.sourceTableCode$

// #bind.field.targetTableCode#
        sb.append("target_table_code,");
// $bind.field.targetTableCode$

        sb.append("create_by,update_by,create_time,update_time");
        sb.append(") VALUES (");
// field ? auto
// #bind.field.sourceFieldCode#
        sb.append("?,"); // source_field_code
// $bind.field.sourceFieldCode$

// #bind.field.targetFieldCode#
        sb.append("?,"); // target_field_code
// $bind.field.targetFieldCode$

// #bind.field.sourceTableCode#
        sb.append("?,"); // source_table_code
// $bind.field.sourceTableCode$

// #bind.field.targetTableCode#
        sb.append("?,"); // target_table_code
// $bind.field.targetTableCode$

        sb.append("?,?,?,?");
        sb.append(")");

        return template.update(sb.toString(),
// field value auto
// #bind.field.sourceFieldCode#
                model.getSourceFieldCode(),
// $bind.field.sourceFieldCode$

// #bind.field.targetFieldCode#
                model.getTargetFieldCode(),
// $bind.field.targetFieldCode$

// #bind.field.sourceTableCode#
                model.getSourceTableCode(),
// $bind.field.sourceTableCode$

// #bind.field.targetTableCode#
                model.getTargetTableCode(),
// $bind.field.targetTableCode$

                model.getCreateBy(),
                model.getUpdateBy(),
                model.getCreateTime(),
                model.getUpdateTime()
        );
    }

    @Override
    public int save(List<EntityColumnRelation> list) {
        StringBuilder sb = new StringBuilder("INSERT INTO bs_entity_column_relation(");
// field column auto
// #bind.field.sourceFieldCode#
        sb.append("source_field_code,");
// $bind.field.sourceFieldCode$

// #bind.field.targetFieldCode#
        sb.append("target_field_code,");
// $bind.field.targetFieldCode$

// #bind.field.sourceTableCode#
        sb.append("source_table_code,");
// $bind.field.sourceTableCode$

// #bind.field.targetTableCode#
        sb.append("target_table_code,");
// $bind.field.targetTableCode$

        sb.append("create_by,update_by,create_time,update_time");
        sb.append(") VALUES (");
// field ? auto
// #bind.field.sourceFieldCode#
        sb.append("?,"); // source_field_code
// $bind.field.sourceFieldCode$

// #bind.field.targetFieldCode#
        sb.append("?,"); // target_field_code
// $bind.field.targetFieldCode$

// #bind.field.sourceTableCode#
        sb.append("?,"); // source_table_code
// $bind.field.sourceTableCode$

// #bind.field.targetTableCode#
        sb.append("?,"); // target_table_code
// $bind.field.targetTableCode$

        sb.append("?,?,?,?");
        sb.append(")");
        return Arrays.stream(template.batchUpdate(sb.toString(),
                list.stream()
                        .map(model -> new Object[]{
// field value auto
// #bind.field.sourceFieldCode#
                                model.getSourceFieldCode(),
// $bind.field.sourceFieldCode$

// #bind.field.targetFieldCode#
                                model.getTargetFieldCode(),
// $bind.field.targetFieldCode$

// #bind.field.sourceTableCode#
                                model.getSourceTableCode(),
// $bind.field.sourceTableCode$

// #bind.field.targetTableCode#
                                model.getTargetTableCode(),
// $bind.field.targetTableCode$

                                model.getCreateBy(),
                                model.getUpdateBy(),
                                model.getCreateTime(),
                                model.getUpdateTime()
                        })
                        .toList()
        )).sum();
    }

    @Override
    public int updateById(EntityColumnRelation model) {
        return updateByCondition(model.getPreCondition().forUpdate().id().eq().toCondition());
    }

    @Override
    public int deleteById(Long id) {
        return deleteByCondition(new EntityColumnRelationFPC().forDelete().id().eq(id).toCondition());
    }

    @Override
    public int deleteById(List<Long> list) {
        return deleteByCondition(new EntityColumnRelationFPC().forDelete().id().in(list).toCondition());
    }

    @Override
    public EntityColumnRelation getById(Long id) {
        return getStreamByCondition(new EntityColumnRelationFPC().id().eq(id).toCondition(), EntityColumnRelation.class).findFirst().orElse(null);
    }

    @Override
    public List<EntityColumnRelation> getListById(List<Long> list) {
        return getStreamByCondition(new EntityColumnRelationFPC().id().in(list).toCondition(), EntityColumnRelation.class).toList();
    }

    @Override
    public EntityColumnRelation getFirst(Condition condition) {
        return getFirst(condition, EntityColumnRelation.class);
    }

    @Override
    public Stream<EntityColumnRelation> getStreamByCondition(Condition condition) {
        return getStreamByCondition(condition, EntityColumnRelation.class);
    }

    @Override
    public Page<EntityColumnRelation> getPageByCondition(Condition condition) {
        return getPageByCondition(condition, EntityColumnRelation.class);
    }
}
